<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php


class MUrlManager
extends MApplicationComponent
{
	private static  $API_VERSION;
	
	private function _getSubFolder(){
		$scriptName = $_SERVER["SCRIPT_NAME"];
		return rtrim(dirname($scriptName),'\\/');
	}
	
	public static function getAPIVersion(){
		return self::$API_VERSION;
	}
	
	public function parseActionFromUrl()
	{
								$uri = $_SERVER['REQUEST_URI'];
		$uri = urldecode($uri);
				$sub = $this->_getSubFolder();
		$sub_length = strlen($sub);
		$uri = substr($uri, $sub_length);
								$index      = strpos($uri, MConst::VERSION_1);
		if($index!=FALSE){
			$uri  = substr($uri, $index+strlen(MConst::VERSION_1));
			self::$API_VERSION = 1;
		}
				$uri  = MUtils::convertStandardPath($uri);
		$parts = array_slice(explode('/', $uri), 1);

								if (count($parts) < 1)
		{
			return false;
		}
		$action  = $parts[0];
		if ($pos = strpos($action, '?'))
		{
			$action = substr($action, 0, $pos);
		}

		$array = array();
		$array["action"]    = $action;
		$array["uri"]       = $uri;
		return $array;
	}

	
	public function parsePathFromUrl($uri) {
		$path = $uri;
								if ($pos = strpos($path, '?'))
		{
			$path = substr($path, 0, $pos);
		}

		$index = strrpos($path, MConst::API);
		$path  = substr($path, $index);
		$parts = array_slice(explode('/', $path), 3);
								if (count($parts) <= 0) {
			return false;
		}
								$path = join("/", $parts);
		return $path;
	}

	
	public function parseRootFromUrl($uri) {
		$path = $uri;
		$index = strrpos($path, MConst::API);
		$path  = substr($path, $index);
		$parts = array_slice(explode('/', $path), 2);
								if (count($parts) <= 0) {
			return false;
		}
								return $parts[0];
	}
}
?>